package com.lt.flashcard.view;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.select.SelectorComposer;

import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.Repository;

public class AdminPageCtrl extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;

	private User user;
	
	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent,
			ComponentInfo compInfo) {
		// TODO Auto-generated method stub
		AAService srv  = Repository.getInstance().getAAService();
		if(!srv.isLogin() || !srv.hasRole("admin")) {
			Executions.sendRedirect("/admin_login.zul");
		}		
		user = srv.getCurrentUser();
		return super.doBeforeCompose(page, parent, compInfo);
	}
	
	public User getUser(){
		return user;
	}
	
	public void setUser(User user){
		this.user = user;
	}
	
//	@Listen("onClick=#btnlogout")
//	public void doLogout() {
//		AAService srv = Repository.getInstance().getAAService();
//		try {
//			srv.doLogout();
//			Executions.sendRedirect("/admin_login.zul");
//		}
//		catch (Exception e) {
//			Messagebox.show(e.getMessage());
//		}
//	}
}
